<?php
/**
 * Scripts and stylesheets
 *
 * Enqueue stylesheets in the following order:
 * 1. /theme/assets/css/main.css
 *
 * Enqueue scripts in the following order:
 * 1. jquery-1.11.1.min.js via Google CDN
 * 2. /theme/assets/js/vendor/modernizr.min.js
 * 3. /theme/assets/js/scripts.js (in footer)
 *
 * Google Analytics is loaded after enqueued scripts if:
 * - An ID has been defined in config.php
 * - You're not logged in as an administrator
 */
function roots_scripts() {
  if (WP_ENV === 'cloud') {
      $assets     = array(
      'css' => 'http://tuicool.qiniudn.com/bootstrap-v8.css',
      'jquery'    => 'http://tuicool.qiniudn.com/jquery-2.1.1.min.js',
      'knockout'=>'http://tuicool.qiniudn.com/knockout-3.2.0.js',
      'plugin'=>'http://tuicool.qiniudn.com/plugins-v3.js',
      'moment'=>'http://tuicool.qiniudn.com/moment-with-locales.min.js',
      'js'  =>get_template_directory_uri().'/assets/js/app.js'
    );
  } else {
    $assets = array(
        'css'=>get_template_directory_uri().'/assets/css/bootstrap-v8.css',
        'jquery'    => get_template_directory_uri().'/assets/js/jquery-2.1.1.min.js',
        'knockout'=>get_template_directory_uri().'/assets/js/knockout-3.2.0.js',
        'plugin'=>get_template_directory_uri().'/assets/js/plugins-v3.js',
        'moment'=>get_template_directory_uri().'/assets/js/moment-with-locales.min.js',
        'js'  =>get_template_directory_uri().'/assets/js/app.js'
      );
  }
  
  wp_enqueue_style('roots_css', $assets['css'], false, null);

  wp_deregister_script('jquery');
  wp_register_script('jquery', $assets['jquery'], array(), null, true);
  add_filter('script_loader_src', 'roots_jquery_local_fallback', 10, 2);
  
  if (is_single() && comments_open() && get_option('thread_comments')) {
    wp_enqueue_script('comment-reply');
  }

  wp_enqueue_script('jquery');
  wp_enqueue_script('plugin',$assets['plugin'],array('jquery'),null,true);
  wp_enqueue_script('moment',$assets['moment'],array(),null,true);
  if (WP_ENV != 'cloud') {
    wp_enqueue_script('knockout',$assets['knockout'],array(),null,true);
    wp_enqueue_script('roots_js', $assets['js'], array('jquery','underscore-js'), null, true);
  }
}
add_action('wp_enqueue_scripts', 'roots_scripts', 100);

function roots_jquery_local_fallback($src, $handle = null) {
  static $add_jquery_fallback = false;

  if ($add_jquery_fallback) {
    echo '<script>window.jQuery || document.write(\'<script src="' . get_template_directory_uri() . '/assets/js/jquery-2.1.1.min.js"><\/script>\')</script>' . "\n";
    $add_jquery_fallback = false;
  }

  if ($handle === 'jquery') {
    $add_jquery_fallback = true;
  }

  return $src;
}
add_action('wp_head', 'roots_jquery_local_fallback');

/**
 * Google Analytics snippet from HTML5 Boilerplate
 * 
 * Cookie domain is 'auto' configured. See: http://goo.gl/VUCHKM
 */
function roots_google_analytics() { ?>
<script>
  <?php if (WP_ENV === 'production') : ?>
    (function(b,o,i,l,e,r){b.GoogleAnalyticsObject=l;b[l]||(b[l]=
    function(){(b[l].q=b[l].q||[]).push(arguments)});b[l].l=+new Date;
    e=o.createElement(i);r=o.getElementsByTagName(i)[0];
    e.src='//www.google-analytics.com/analytics.js';
    r.parentNode.insertBefore(e,r)}(window,document,'script','ga'));
  <?php else : ?>
    function ga() {
      console.log('GoogleAnalytics: ' + [].slice.call(arguments));
    }
  <?php endif; ?>
  ga('create','<?php echo GOOGLE_ANALYTICS_ID; ?>','auto');ga('send','pageview');
</script>

<?php }
if (GOOGLE_ANALYTICS_ID && (WP_ENV !== 'production' || !current_user_can('manage_options'))) {
  add_action('wp_footer', 'roots_google_analytics', 20);
}
